package chinaums.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import chinaums.dao.LongUrlLinkMapper;
import chinaums.entity.LongUrlLink;
import chinaums.service.IUrlService;
import chinaums.service.LongUrlLinkService;
/**
 * <p>Title:LongUrlLinkServiceImpl</p>
 * <p>Description:长链接接口实现</p>
 * @version v1.0
 * @author RenLei
 * 2017年8月1日
 */
@Service
public class LongUrlLinkServiceImpl implements LongUrlLinkService{
	private static Logger log= LoggerFactory.getLogger(LongUrlLinkServiceImpl.class);
	@Autowired
	private LongUrlLinkMapper mapper;
    @Autowired 
    private IUrlService urlService;
    
    @Override
	public void addUrl() {
    	try {
			List<LongUrlLink> linkList=mapper.selectLongUrl();
			//判断集合是否为空，空则不需要转换
			if(linkList==null||linkList.size()<=0){
				log.info("没有需要转换的长链接!");
				return;
			}
			log.info("这次需要转换的长链接的数量是:"+linkList.size());
			//数量
			int listSize = linkList.size();
			//线程个数
			int RunSize=10;
			//定义数量
			int count=0;
			//判断集合数是否大于线程数，小于则线程为1，count等于集合大小
			if(listSize<RunSize){
				RunSize=1;
				count=listSize;
			}else{
				//每个线程执行的数量
				count=listSize/RunSize;
			}
			//创建线程池
			ExecutorService fixedThreadPool=Executors.newFixedThreadPool(RunSize);
			//创建线程辅助类
			CountDownLatch countDownLatch = new CountDownLatch(RunSize);
			List<LongUrlLink> newList=new ArrayList<LongUrlLink>();
			//分割数据，避免线程拿到重复数据，将数据分页
			for (int j = 0; j < RunSize; j++) {
			    if((j+1)==RunSize){
			        int startIndex = (j*count);
			        int endIndex = linkList.size();
			        newList =linkList.subList(startIndex,endIndex);
			    }else{
			        int startIndex = j*count;
			        int endIndex = (j+1)*count;
			        newList =linkList.subList(startIndex,endIndex);
			    }
			    log.info("每个线程执行的长度是:"+newList.size());
			    //创建一个线程类，将数据分割，然后每个线程执行相应的任务
			    UrlRunnable urlrun=new UrlRunnable(newList,urlService,mapper,countDownLatch);
			    //执行线程
			    fixedThreadPool.execute(urlrun);
			}
			try {
				//等待所有线程执行完
				countDownLatch.await();
				//关闭线程
				fixedThreadPool.shutdown();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			log.info("长短转换出异常了!");
			e.printStackTrace();
		}
        
	}
    
    
	@Override
	public int insertLongUrl(List<LongUrlLink> linkList) {
		return mapper.insertLongUrl(linkList);
	}


	@Override
	public List<LongUrlLink> selectLongUrl() {
		return mapper.selectLongUrl();
	}


	@Override
	public int updateLongUrl(LongUrlLink link) {
		return mapper.updateLongUrl(link);
	}


	@Override
	public int updateLongVersion(LongUrlLink link) {
		return mapper.updateLongVersion(link);
	}

	@Override
	public int updateErrorVersion() {
		return mapper.updateErrorVersion();
	}
	
}
